<template>
	<view class="page">
		<view class="detail-box-item">
			<view class="title">
				基础信息
			</view>
			<view class="content-box">
				<view class="cell-item">
					<view class="left-box">
						采购员编号
					</view>
					<view class="right-box">
						{{info.purchaseNo}}
					</view>
				</view>
				<view class="cell-item">
					<view class="left-box">
						采购优先级
					</view>
					<view class="right-box" :style="{'color':gradeList[info.priority]}">
						{{info.priority}}级紧急
					</view>
				</view>
				<view class="cell-item">
					<view class="left-box">
						采购订单状态
					</view>
					<view class="right-box" v-if="statusList[info.status]" :style="{'color': statusList[info.status].color}">
						{{statusList[info.status].name}}
					</view>
				</view>
				<view class="cell-item">
					<view class="left-box">
						申请时间
					</view>
					<view class="right-box">
						{{info.applyTime}}
					</view>
				</view>
			</view>
		</view>
		<view class="detail-box-item">
			<view class="title">
				采购物料
			</view>
			<view class="card-item" v-for="(item,index) in info.purchaseMaterialVOS" :key="index">
				<view class="product-item" @click="materialOpen(item)">
					<view class="img-box">
						<image class="img" src="/static/image/img_icon.png" mode="aspectFit"></image>
					</view>
					<view class="right-box">
						<view class="product-title">
							{{item.materialShortName}}
						</view>
						<view class="product-id">
							{{item.materialNo}}
						</view>
						<view class="size">
							{{item.paramstr}}
						</view>
						<view class="sign">
							{{item.unit}}{{item.twoUnit ? '/' + item.twoUnit : ''}}
						</view>
					</view>
				</view>
				<view class="table-box">
					<view class="table-row">
						<view class="table-item">
							<view class="item-value">
								<view class="table-title">
									请购数：
								</view>
								<view class="table-value">
									<view class="num">
										{{item.presentCount}}
									</view>
									<view class="sign">
										/{{item.unit}}
									</view>
								</view>
							</view>
						</view>
						<view class="table-item">
							<view class="item-value">
								<view class="table-title">
									提购数：
								</view>
								<view class="table-value">
									<view class="num">
										{{item.purchaseCount}}
									</view>
								</view>
							</view>
						</view>
						<view class="table-item">
							<view class="item-value">
								<view class="table-title">
									提购价格：
								</view>
								<view class="table-value">
									<view class="num">
										{{item.purchasePrice}}
									</view>
								</view>
							</view>
						</view>
					</view>
					<view class="table-row">
						<view class="table-item">
							<view class="item-value">
								<view class="table-title">
									含税单价：
								</view>
								<view class="table-value">
									<view class="num">
										{{item.gstInclusive}}
									</view>
								</view>
							</view>
						</view>
						<view class="table-item">
							<view class="item-value">
								<view class="table-title">
									含税总价：
								</view>
								<view class="table-value">
									<view class="num">
										{{item.gstInclusiveCount}}
									</view>
								</view>
							</view>
						</view>
						<view class="table-item">
							<view class="item-value">
								<view class="table-title">
									税率：
								</view>
								<view class="table-value">
									<view class="num">
										{{item.taxRate}}
									</view>
								</view>
							</view>
						</view>
					</view>
					<view class="table-row">
						<view class="table-item">
							<view class="item-value">
								<view class="table-title">
									不含税单价：
								</view>
								<view class="table-value">
									<view class="num">
										{{item.taxExcluded}}
									</view>
								</view>
							</view>
						</view>
						<view class="table-item">
							<view class="item-value">
								<view class="table-title">
									不含税总价：
								</view>
								<view class="table-value">
									<view class="num">
										{{item.taxExcludedCount}}
									</view>
								</view>
							</view>
						</view>
					</view>
					<view class="table-row">
						<view class="table-item">
							<view class="item-value">
								<view class="table-title">
									含税单价：
								</view>
								<view class="table-value">
									<view class="num">
										{{item.gstInclusive}}
									</view>
								</view>
							</view>
						</view>
						<view class="table-item">
							<view class="item-value">
								<view class="table-title">
									含税总价：
								</view>
								<view class="table-value">
									<view class="num">
										{{item.gstInclusiveCount}}
									</view>
								</view>
							</view>
						</view>
						<view class="table-item">
							
						</view>
					</view>
				</view>
				<view class="item-value" v-if="item.projectName">
					<view class="table-title">
						项目名称：
					</view>
					<view class="table-value">
						<view class="num">
							{{item.projectName ? item.projectName : ''}}
						</view>
					</view>
				</view>
				<view class="item-value">
					<view class="table-title">
						期望到货时间：
					</view>
					<view class="table-value">
						<view class="num" style="color:red;">
							{{item.expectArrivalTime}}
						</view>
					</view>
				</view>
				<view class="button" @click="batchTap(item)">
					查看批次
				</view>
			</view>
		</view>
		<view class="clearbottom"></view>
		<view class="button-box" v-if="info.status == '0'">
			<view class="button reject" @click="rejectTap">
				拒绝
			</view>
			<view class="button agree" @click="agreeTap">
				通过
			</view>
		</view>
		<view class="reject-reason-box" v-if="info.status == '2'">
			<view class="left-box">
				退回原因
			</view>
			<view class="right-box">
				{{info.advice}}
			</view>
		</view>
		<remark v-if="remarkType" title="退回原因" @cancel="rejectCancel" @inputChange="rejectInputChange" @confirm="rejectConfirm"></remark>
		<batch v-if="batchType" :info="item" @close="batchClose"></batch>
		<material :info="item" v-if="materialType" @close="materialClose"></material>
	</view>
</template>

<script>
	import * as api from '@/utils/api.js';
	import material from '@/subcontractList/order/purchase/component/dialog/material';
	import remark from '@/pages/mask/component/remark/remark';
	import batch from '@/subcontractList/order/purchase/component/dialog/batch';
	export default {
		components:{
			remark,batch,material
		},
		data() {
			return {
				id:null,// 订单id
				statusList:[ // 采购订单状态列表
						{name:'待审核',color:'#FB3835'},
						{name:'已审核',color:'#63C05A'},
						{name:'已退回',color:'#FB882A'},
						{name:'待接单',color:'#FB3835'},
						{name:'采购中',color:'#FB3835'},
						{name:'被退回',color:'#FB882A'}
					],
				item:{}, // 选中物料信息
				info:{}, // 采购详情信息
				rejectmessage:'',// 拒绝原因输入信息
				remarkType:false, // 拒绝原因弹窗显示状态
				batchType:false,// 批次弹窗显示状态
				materialType:false, // 物料信息弹窗显示状态
				gradeList:['','#DA2B55','#F93C33','#FE940A','#F6B45F','#6D7278'], // 等级颜色列表
				
			}
		},
		onLoad(option){
			if(option.id){
				this.id = option.id;
				this.init(option.id);
			}
		},
		methods: {
			init:function(id){
				api.purchaseDetail({id:id}).then(res=>{
					res.data.priority = res.data.priority.substring(0, 1);
					
					let str = '';
					res.data.purchaseMaterialVOS.forEach(result=>{
						result.paramArr = [];
						str = '';
						result.param = JSON.parse(result.param);
						for(let i  in result.param){
							result.paramArr.push({'unit':i,'value':result.param[i]});
							str = str + i+ '：'+result.param[i]+ '/';
						}
						str = str.substring(0,str.length - 1);
						result.paramstr = str;
					})
					this.info = res.data;
					console.log(222,res.data)
				})
			},
			/**
			 * 拒绝按钮点击触发事件
			 */
			rejectTap:function(){
				this.remarkType = true;
			},
			/**
			 * 监听拒绝原因输入框触发事件
			 */
			rejectInputChange:function(str){
				this.rejectmessage = str;
			},
			/**
			 * 拒绝原因提交触发事件
			 */
			rejectConfirm:function(e){
				let that = this;
				let id = that.id;
				let data = {"advice":this.rejectmessage,"applyId":id,"status":"2"};
				api.purchaseApproval(data).then(res=>{
					uni.showToast({
						title: '审批成功',
						mask:true,
						duration: 2000,
						complete:function(res){
							that.info = {};
							that.init(id);
							getApp().globalData.refreshPage = 'pendingapproval';
						}
					});
				})
			},
			/**
			 * 拒绝原因悬浮窗去掉点击触发事件
			 */
			rejectCancel:function(){
				this.remarkType = false;
			},
			/**
			 * 通过按钮触发事件
			 */
			agreeTap:function(){
				let that = this;
				let id = that.id;
				let data = {"advice":"","applyId":id,"status":"1"};
				uni.showModal({
					title: '提示',
					content: '确认通过？',
					success: function (res) {
						if (res.confirm) {
							api.purchaseApproval(data).then(result=>{
							uni.showToast({
								title: '审批成功',
								mask:true,
								duration: 2000,
								complete() {
									getApp().globalData.refreshPage = 'pendingapproval';
									setTimeout(function(){
										that.init(id);
									},2000)
									
								}
							});
						});
						} else if (res.cancel) {
							
						}
					}
				});
			},
			materialOpen:function(item){
				this.item = item;
				this.materialType = true;
			},
			materialClose:function(){
				this.materialType = false;
			},
			batchTap:function(item){
				this.item = item;
				if(item.materialBatchDTOs.length > 0){
					this.batchType = true;
				}else{
					uni.showToast({
						title: '暂无批次信息',
						icon:'none',
						duration: 2000
					});
				}
				
			},
			batchClose:function(){
				this.batchType = false;
			},
		}
	}
</script>

<style lang="scss" scoped>
	.page {
		background-color: #eaeaea;
		min-height: 100vh;
	}

	.detail-box-item {
		box-sizing: border-box;

		.title {
			font-size: 32rpx;
			font-weight: bold;
			padding: 20rpx;
			padding-left: 20rpx;
			margin-left: 20rpx;
			position: relative;
			line-height: 60rpx;
		}

		.title::after {
			content: '';
			width: 6rpx;
			top: 35%;
			height: 30%;
			position: absolute;
			left: 0;
			background-color: #29ac7a;
		}

		.content-box {
			background-color: white;
			padding: 0 20rpx;

			.cell-item {
				display: flex;
				justify-content: space-between;
				line-height: 60rpx;
				font-size: 28rpx;

				.left-box {
					color: #818181;
				}
			}
		}

		.card-item {
			width: calc(100% - 40rpx);
			margin: 0 auto;
			box-sizing: border-box;
			background-color: white;
			padding: 20rpx;
			position: relative;
			.product-item {
				display: flex;

				.img-box {
					width: 160rpx;
					height: 160rpx;
					background-color: #eeeeee;
					overflow: hidden;

					.img {
						width: 50%;
						height: 50%;
						display: block;
						margin: 40rpx auto;
					}
				}

				.right-box {
					font-size: 26rpx;
					line-height: 40rpx;
					margin-left: 20rpx;

					.product-title {
						color: #ff5400;
						font-weight: bold;
					}

					.sign {
						color: #634ed1;
					}
				}
			}

			.table-box {
				display: table;
				width: 100%;

				.table-row {
					display: table-row;

					.table-item {
						// width: calc(100% / 3);

						display: table-cell;

					}
				}
			}

			.item-value {
				display: flex;
				font-size: 24rpx;
				line-height: 40rpx;

				.table-title {
					white-space: nowrap;
				}

				.table-value {
					display: flex;

					.num {
						color: #634ed1;
					}
				}
			}
			.button{
				position: absolute;
				bottom: 20rpx;
				right: 20rpx;
				width: fit-content;
				padding: 0 25rpx;
				height:70rpx;
				line-height: 70rpx;
				border-radius: 10rpx;
				color: white;
				font-size: 26rpx;
				text-align: center;
				background-color: #35af86;
			}
		}
	}
	.clearbottom{
		width: 100%;
		height: calc(constant(safe-area-inset-bottom) + 100rpx);
		height: calc(env(safe-area-inset-bottom) + 100rpx);
	}
	.button-box{
		display: flex;
		width: calc(100% - 40rpx);
		position: fixed;
		left: 20rpx;
		bottom: calc(constant(safe-area-inset-bottom) + 20rpx);
		bottom: calc(env(safe-area-inset-bottom) + 20rpx);
		.button{
			color: white;
			border-radius: 10rpx;
			line-height: 70rpx;
			height: 70rpx;
			width: calc(100% - 10rpx);
			text-align: center;
			margin-right: 20rpx;
		}
		.reject{
			background-color: #afafaf;
		}
		.agree{
			background-color: #4169e2;
		}
		.button:last-child{
			margin-right: 0;
		}
	}
	.reject-reason-box{
		display: flex;
		position: fixed;
		width: 100%;
		left:0rpx;
		padding: 0 20rpx;
		box-sizing: border-box;
		bottom: calc(constant(safe-area-inset-bottom));
		bottom: calc(env(safe-area-inset-bottom));
		background-color: white;
		justify-content: space-between;
		line-height:80rpx;
		font-size: 28rpx;
	}
</style>
